package com.mhkj.base.messageRemind.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.messageRemind.entity.MessageRemind;
import com.mhkj.base.messageRemind.service.MessageRemindService;
import com.mhkj.common.util.ScheduledImpl;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.rbac.entity.Dictionary;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 消息提醒
 * @author wk
 * @date 2018年8月7日
 * @version 1.0
 */
@Controller
@RequestMapping("/messageRemind")
public class MessageRemindController extends BaseController<MessageRemind>{
	@Resource
	private MessageRemindService messageRemindService;
	@Autowired
	private OrgService orgService;
	@Autowired
	private DictionaryService dictionaryService;
	
	@Resource
	private ScheduledImpl scheduledImpl;
	
	@InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
	 
	@Resource
	public void setMessageRemindService(MessageRemindService messageRemindService) {
		super.setBaseService(messageRemindService);
	}
	
	@RequestMapping("/listByPage")
	@ResponseBody
	public Pagination<MessageRemind> listByPage(MessageRemind messageRemind,int pageIndex,int pageSize){
		Pagination<MessageRemind> wPagination = messageRemindService.listByPage(messageRemind,pageIndex,pageSize);
		
		List<MessageRemind> data = wPagination.getData();
		
		for (MessageRemind entity : data) {
			
			Long remindFrequency = Long.parseLong(entity.getRemindFrequency());//提醒频次
			if(!StringUtils.isEmpty(remindFrequency)) {
				Dictionary dict = dictionaryService.getById(remindFrequency);
				entity.setRemindFrequencyName(dict.getValue());
			}
			
			 Long remindMode = Long.parseLong(entity.getRemindMode());//提醒方式
			if(!StringUtils.isEmpty(remindMode)) {
				Dictionary dict = dictionaryService.getById(remindMode);
				entity.setRemindModeName(dict.getValue());
			}
			 String remindType = entity.getRemindType();//提醒类型
			 String[] remindTypeArr= remindType.split(",");
			if(!StringUtils.isEmpty(remindTypeArr)) {
				String remindTypeName = null;
				for (int i = 0; i < remindTypeArr.length; i++) {
					Dictionary dict = dictionaryService.getById(Long.parseLong(remindTypeArr[i]));
					if(remindTypeName!=null) {
						remindTypeName=remindTypeName+","+dict.getValue();
					}else {
						remindTypeName=dict.getValue();
					}
				}
				entity.setRemindTypeName(remindTypeName);
			}
			
			Employee employeeInfo = orgService.employeeInfo(entity.getCreateBy());
			entity.setCreateName(employeeInfo.getName());
		}
		return wPagination;
	}
	
	@Override
	@RequestMapping("/save")
	@ResponseBody
	public ResponseMessage save(MessageRemind t) {
		//t.getSendTime().split(":");
		//scheduledImpl.setCron("1 03 16 ? * *");
		
		//scheduledImpl.stopCron();
		return super.save(t);
	}

	@Override
	@RequestMapping("/edit")
	@ResponseBody
	public ResponseMessage edit(MessageRemind t) {
		return super.edit(t);
	}

	@Override
	@RequestMapping("/del")
	@ResponseBody
	public ResponseMessage del(MessageRemind t) {
		return super.del(t);
	}

	@Override
	@RequestMapping("/info")
	@ResponseBody
	public MessageRemind info(long id) {
		return super.info(id);
	}
	
	
}
